package com.whut.welcomepage.fragment;


import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.whut.welcomepage.R;
import com.whut.welcomepage.adapter.WelFragPagerAdapter;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 * 欢迎引导页
 */

public class WelcomeFragment extends Fragment {
    private List<ImageView> mList = new ArrayList<>();
    private ViewPager viewPager_welFragment;
    private RadioGroup radioGroup_welFragment;
    private RadioButton[] radioButtons;
    private Button button_jump;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                int currentIndex = msg.arg1;
                viewPager_welFragment.setCurrentItem(currentIndex);
            }
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_welcome, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewPager_welFragment = (ViewPager) view.findViewById(R.id.viewPager_welFragment);
        radioGroup_welFragment = (RadioGroup) view.findViewById(R.id.radioGroup_welFragment);
        button_jump = (Button) view.findViewById(R.id.button_jump);
        initData();
        initUI();
    }

    private void initUI() {
        WelFragPagerAdapter adapter = new WelFragPagerAdapter(mList);
        viewPager_welFragment.setAdapter(adapter);
        viewPager_welFragment.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                radioButtons[position % mList.size()].setChecked(true);
                //                当滑到第4个,设置按钮为显示
                button_jump.setVisibility(((position % mList.size() == 3) ? View.VISIBLE : View
                        .GONE));
                //                if (position % mList.size() == 3) {
                //                    button_jump.setVisibility(View.VISIBLE);
                //                } else {
                //                    button_jump.setVisibility(View.GONE);
                //                }
            }
        });

        //        自动轮播
        final AutoRunThread autoRunThread = new AutoRunThread();
        autoRunThread.startRun();
        //        为viewPager添加触碰监听，触碰停止滑动状态
        viewPager_welFragment.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        autoRunThread.stopRun();
                        break;
                    case MotionEvent.ACTION_CANCEL:
                    case MotionEvent.ACTION_UP:
                        autoRunThread.startRun();
                        break;
                }
                return false;
            }
        });
        viewPager_welFragment.setCurrentItem(1000);
    }

    private void initData() {
        TypedArray array = getResources().obtainTypedArray(R.array.images);
        radioButtons = new RadioButton[array.length()];
        for (int i = 0; i < array.length(); i++) {
            ImageView imageView = new ImageView(getContext());
            imageView.setBackground(array.getDrawable(i));
            mList.add(imageView);
            //            添加小圆点
            radioButtons[i] = new RadioButton(getContext());
            //            设置图片
            radioButtons[i].setButtonDrawable(R.drawable.dot);
            radioButtons[i].setPadding(0, 0, 10, 0);
            radioGroup_welFragment.addView(radioButtons[i]);
        }
        radioButtons[0].setChecked(true);
    }

    class AutoRunThread implements Runnable {
        private long delayTime = 2000;
        private boolean isRun = false;

        @Override
        public void run() {
            //            获取当前viewPager的下标
            int currentItem = viewPager_welFragment.getCurrentItem();
            Message message = Message.obtain();
            message.what = 1;
            message.arg1 = ++currentItem;
            handler.sendMessage(message);
            //            实现轮播
            handler.postDelayed(this, delayTime);
        }

        //        开始
        public void startRun() {
            //            if (!isRun) {
            //                isRun = true;
            handler.removeCallbacks(this);
            handler.postDelayed(this, delayTime);
            //            }
        }

        //        停止
        public void stopRun() {
            //            if (isRun) {
            //                isRun = false;
            handler.removeCallbacks(this);
            //            }
        }
    }


}
/*
* xml绘制图形注意设置size
*
* View point =  new View(getContext());
* point.setBackgroundResource(R.drawable.dot);
* ViewPager.LayoutParams params=new ViewPager.LayoutParams();
* DisplayMetrics metrics = new DisplayMetrics();
* float dimension = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 10, metrics);
* params.height= (int) dimension;
* params.width= (int) dimension;
* point.setPadding(20,20,20,20);
* point.setLayoutParams(params);
* layout_point.addView(point);
* */